package com.frx.leetcode.classify.link;

import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author ：frx
 * @date ：Created in 2019/5/6 22:05
 * @description： 两个链表逆序相加
 *
 *                输入：(2 -> 4 -> 3) + (5 -> 6 -> 4)
 *                输出：7 -> 0 -> 8
 *                原因：342 + 465 = 807
 * @modified By：
 * @version: ToDO
 */
public class NodeAdd {

    public static void main(String[] args){
        ConcurrentHashMap map = new ConcurrentHashMap();
        HashMap map2 = new HashMap();
        ListNode ln = new ListNode(2);
        ListNode ln2 = new ListNode(4);
        ln.next = ln2;
        ln.next.next = new ListNode(3);

        while (true){
            if (ln.next == null){
                System.out.println(ln.val);
                System.out.println("遍历完成");
                break;
            }
            System.out.println(ln.val);
            ln = ln.next;
        }

    }

    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode pre = new ListNode(0);
        ListNode cur = pre.next;
        int temp = 0;
        while (l1 != null || l2 != null){

            int a = l1 == null ? 0 : l1.val;
            int b = l2 == null ? 0 : l2.val;
            cur.next = new ListNode((a + b + temp)%10);
            temp = (a+b+temp)/10;
            cur = cur.next;
            if (l1 != null){
                l1 = l1.next;
            }
            if (l2 != null){
                l2 = l2.next;
            }
        }
        if (temp == 1){
            cur.next = new ListNode(temp);
        }

        return pre.next;
    }
}

